package com.sunmouse.toolkit.fragment.other;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.google.zxing.encode.CaptureActivity;
import com.sunmouse.toollib.utils.FileUtils;
import com.sunmouse.toollib.utils.JumpUtils;
import com.sunmouse.toollib.utils.ThreadPoolUtils;
import com.sunmouse.toolkit.R;
import com.sunmouse.toolkit.fragment.BaseFragment;
import com.sunmouse.toolkit.util.QRCodeUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class QRCodeFragment extends BaseFragment {
    @BindView(R.id.scan_btn)
    Button scanBtn;
    @BindView(R.id.generate_btn)
    Button generateBtn;
    @BindView(R.id.img_iv)
    ImageView imgIv;
    Unbinder unbinder;

    public static final String desPath = FileUtils.getStoragePath() + "/qr123.png";


    public QRCodeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.activity_qrcode, container, false);
        unbinder = ButterKnife.bind(this, v);
        return v;
    }


    @Override
    public void init() {
        this.scanBtn.setOnClickListener(this);
        this.generateBtn.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == scanBtn) {
//            JumpUtils.simpleJump(getActivity(), CaptureActivity.class);
            JumpUtils.jumpWithResult(getActivity(),CaptureActivity.class,5);
            return;
        }
        if (v == generateBtn) {

            ThreadPoolUtils.execute(new Runnable() {
                @Override
                public void run() {
                    String content = "https://www.baidu.com";
                    String logoUrl = "dksjd";
                    try {
                        QRCodeUtil.createImage(content, logoUrl, desPath, getActivity());
                        mHandler.sendEmptyMessage(0);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            return;
        }
    }


    @Override
    public void onHandleMessage(Message msg) {
        super.onHandleMessage(msg);
        Bitmap b= BitmapFactory.decodeFile(desPath);
        imgIv.setImageBitmap(b);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
